package admin.dao;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import viewmodel.GamesEditStatus;

import model.Connect;
import model.Game;

public class GameDAO {
	
	public static List<Game> getAllGame(){
		ArrayList<Game> listGame = new ArrayList<Game>();
		String sql = "SELECT * FROM eonl_game ORDER BY id DESC";
		try{
			Statement state = Connect.getConnection().createStatement();
			ResultSet result = state.executeQuery(sql);
			
			while(result.next()){
				Game game = new Game(result.getInt("id"),result.getString("name"),result.getString("username"), result.getString("game"));
				listGame.add(game);
				}
			
			result.close();
			state.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return listGame;
	}

	public static void removeGame(Game game) {
		String sql = "DELETE FROM `eonl_game` WHERE id=" + game.getId();
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void updateGame(GamesEditStatus ges) {
		String sql ="UPDATE `eonl_game`"+" SET name = '"+ges.getGame().getName()+"', username='"+ges.getGame().getUsername()+"', game='"+ges.getGame().getFile()+"' WHERE id="+ges.getGame().getId();
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
		}catch (Exception e) {
			e.printStackTrace();
		}	
	}

	public static void addGame(Game game) {
		String sql = "INSERT INTO `eonl_game` (name,username,game) VALUES ('"+game.getName()+"','"+game.getUsername()+"','"+game.getFile()+"')";
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
		}catch (Exception e) {
			e.printStackTrace();
		}	
	}
}
